﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class ThanhToanVayNganHang : System.Web.UI.Page
{
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmthanhtoannganhang";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;
    static int PageNumber = 10;
    static int CurrentPage = 1;
    double tienlaiphaitra=0;
    double tiengocphaitra = 0;
    string tienphaitra;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            gstGetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {


                string idnguoidung = Session["IDUser"].ToString();
                string sql = "select SuDung,Them,Sua,Xoa,InBaoCao,XemTatCa from XDAPhanQuyen where IDnguoidung='" + idnguoidung + "' and tag='" + frmName + "'";


                DataTable dt2 = ex.GetData_Text(sql);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                    f_InBaoCao = int.Parse(dt2.Rows[0]["InBaoCao"].ToString());
                    f_XemTatCa = int.Parse(dt2.Rows[0]["XemTatCa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    CurrentPage = 1;



                    Title = "Thanh toán vay ngân hàng | " + Header;

                    btnCancel_Click(sender, e);
                    LoadNganHang_TK();
                    int year1 = DateTime.Now.Year - 3;
                    int year2 = DateTime.Now.Year;
                    for (int i = year1; i <= year2; i++)
                    {
                        dlNam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                    }
                    dlNam.SelectedValue = year2.ToString();
                   
                }
                else
                    gstGetMess("Bạn không có quyền xem trang này", "Home.aspx");

            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (f_Them.Equals(1))
        {
            try
            {
                p = new SqlParameter[11];
                p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                p[2] = new SqlParameter("@SoTienLai", double.Parse(txttienlaiphaitra.Text.Trim().Replace(",", "").Equals("") ? "0" : txttienlaiphaitra.Text.Trim()));
                p[2].SqlDbType = SqlDbType.Float;
                p[3] = new SqlParameter("@SoTienGoc", double.Parse(txttiengocphaitra.Text.Trim().Replace(",", "").Equals("") ? "0" : txttiengocphaitra.Text.Trim()));
                p[3].SqlDbType = SqlDbType.Float;
                p[4] = new SqlParameter("@NgayThanhToanLai",ex.GetNgayThang(txtngaythanhtoanlai.Text.Trim()));
                p[4].SqlDbType = SqlDbType.SmallDateTime;

                p[5] = new SqlParameter("@NgayThanhToanGoc", ex.GetNgayThang(txtngaythanhtoantiengoc.Text.Trim()));
                p[5].SqlDbType = SqlDbType.SmallDateTime;
                p[6] = new SqlParameter("@HinhThucLai", drhinhthucthanhtoanlai.SelectedValue);
                p[6].SqlDbType = SqlDbType.Int;

                p[7] = new SqlParameter("@HinhThucGoc",drhinhthucthanhtoangoc.SelectedValue);
                p[7].SqlDbType = SqlDbType.Int;
                p[8] = new SqlParameter("@TaiKhoanLai", ex.GetGuid(drtaikhoanthanhtoanlai.SelectedValue));
                p[8].SqlDbType = SqlDbType.UniqueIdentifier;
                p[9] = new SqlParameter("@TaiKhoanGoc", ex.GetGuid(drhinhthucthanhtoangoc.SelectedValue));
                p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                p[10] = new SqlParameter("@Del",1);
                p[10].SqlDbType = SqlDbType.Int;


                ex.ExecuteNonQuerry("sp_XDAThanhToanVayNganHang_Add", p);
                btnCancel_Click(sender, e);
                gstGetMess("Lưu thành công", "");
                
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tGiá Cước vận chuyển\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền thêm dữ liệu trên trang này", "");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", 1);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;
        GV.DataSource = ex.GetData("sp_xdathanhtoanvaynganhang_List",p);
        GV.DataBind();
        dlnganhang.SelectedIndex = 0;
        drhinhthucthanhtoangoc.SelectedIndex = 0;
        drhinhthucthanhtoanlai.SelectedIndex = 0;
        drtaikhoanthanhtoanlai.SelectedIndex = 0;
        drhinhthucthanhtoangoc.SelectedIndex = 0;
        txttiengoc.Text = "";
        txttiengocdatra.Text = "";
        txttiengocphaitra.Text = "";
        txtsotienconlai.Text = "";
        txttienlaidatra.Text = "";
        txttienlaithangnay.Text = "";
        txttienlaiphaitra.Text = "";
        txttiengocdatra.Text = "";
        TextBox1.Text = "";
        txttiengocthangnay.Text = "";
        dlthang.SelectedIndex = 0;
        txtsothanhtoantiengoc.Text = "";
        txtsothanhtoantienlai.Text = "";
        txtngaythanhtoantiengoc.Text = "";
        txtngaythanhtoanlai.Text = "";
       
        LoadTK();
        LoadTK1();
        LoadNganHang();
       
        btnSave.Visible = true;
        btncapnhat.Visible = false;
        btnNext.Visible = true;
        btnPre.Visible = true;
        btnFirst.Visible = true;
    }

    void LoadTK()
    {
        drtaikhoanthanhtoangoc.Items.Clear();
        drtaikhoanthanhtoangoc.DataSource = ex.GetData("sp_ListTaiKhoan_KhongVAT");
        drtaikhoanthanhtoangoc.DataBind();
        drtaikhoanthanhtoangoc.Items.Insert(0, new ListItem("--Chọn tài khoản--", ""));
        drtaikhoanthanhtoangoc.SelectedIndex = 0;
    }

    void LoadTK1()
    {
        drtaikhoanthanhtoanlai.Items.Clear();
        drtaikhoanthanhtoanlai.DataSource = ex.GetData("sp_ListTaiKhoan_KhongVAT");
        drtaikhoanthanhtoanlai.DataBind();
        drtaikhoanthanhtoanlai.Items.Insert(0, new ListItem("--Chọn tài khoản--", ""));
        drtaikhoanthanhtoanlai.SelectedIndex = 0;
    }

    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", gID);
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        if (e.CommandName.Equals("Select"))
        {
            DataTable dt = ex.GetData("sp_xdathanhtoanvaynganhang_ListByID", p);
            if (dt.Rows.Count > 0)
            {
                try
                {
                    

                    dlnganhang.SelectedValue = dt.Rows[0]["nganhang"].ToString();
                    drhinhthucthanhtoangoc.SelectedValue = dt.Rows[0]["hinhthucgoc"].ToString();
                    drhinhthucthanhtoanlai.SelectedValue = dt.Rows[0]["hinhthuclai"].ToString();
                    drtaikhoanthanhtoangoc.SelectedValue = dt.Rows[0]["taikhoangoc"].ToString();
                    drtaikhoanthanhtoanlai.SelectedValue = dt.Rows[0]["taikhoanlai"].ToString();
                   
                    txtsothanhtoantienlai.Text = dt.Rows[0]["sotienlai"].ToString();
                    txtsothanhtoantiengoc.Text = dt.Rows[0]["sotiengoc"].ToString();
                    txtngaythanhtoanlai.Text = dt.Rows[0]["ngaythanhtoanlai"].ToString();
                    txtngaythanhtoantiengoc.Text = dt.Rows[0]["ngaythanhtoangoc"].ToString();
                   
                }
                catch (Exception ax)
                {
                    gstGetMess("Có lỗi trong quá trình xem, xem trong file log", "");
                    ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tGiá Cước vận chuyển\t" + ax.Message);
                }
               

            }
            btncapnhat.Visible = true;
            btnSave.Visible = false;
        }
        else
            if (e.CommandName.Equals("Del"))
            {
                if (f_Xoa.Equals(1))
                {
                    try
                    {
                        ex.ExecuteNonQuerry("sp_xdathanhtoanvaynganhang_Del", p);
                        btnCancel_Click(sender, e);
                        gstGetMess("Xóa thành công", "");
                    }
                    catch (Exception ax)
                    {
                        gstGetMess("Có lỗi trong quá trình xóa, xem trong file log", "");
                        ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tGiá Cước vận chuyển\t" + ax.Message);

                    }
                }
                else
                    gstGetMess("Bạn không có quyền xóa dữ liệu trên trang này", "");
            }
    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        if (f_Sua.Equals(1))
        {
            try
            {
                Guid gID = new Guid(hdID.Value);
                p = new SqlParameter[11];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;

                p[1] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                p[2] = new SqlParameter("@SoTienLai", double.Parse(txttienlaiphaitra.Text.Trim().Replace(",", "").Equals("") ? "0" : txttienlaiphaitra.Text.Trim()));
                p[2].SqlDbType = SqlDbType.Float;
                p[3] = new SqlParameter("@SoTienGoc", double.Parse(txttiengocphaitra.Text.Trim().Replace(",", "").Equals("") ? "0" : txttiengocphaitra.Text.Trim()));
                p[3].SqlDbType = SqlDbType.Float;
                p[4] = new SqlParameter("@NgayThanhToanLai", ex.GetNgayThang(txtngaythanhtoanlai.Text.Trim()));
                p[4].SqlDbType = SqlDbType.SmallDateTime;

                p[5] = new SqlParameter("@NgayThanhToanGoc", ex.GetNgayThang(txtngaythanhtoantiengoc.Text.Trim()));
                p[5].SqlDbType = SqlDbType.SmallDateTime;
                p[6] = new SqlParameter("@HinhThucLai", drhinhthucthanhtoanlai.SelectedValue);
                p[6].SqlDbType = SqlDbType.Int;

                p[7] = new SqlParameter("@HinhThucGoc", drhinhthucthanhtoangoc.SelectedValue);
                p[7].SqlDbType = SqlDbType.Int;
                p[8] = new SqlParameter("@TaiKhoanLai", ex.GetGuid(drtaikhoanthanhtoanlai.SelectedValue));
                p[8].SqlDbType = SqlDbType.UniqueIdentifier;
                p[9] = new SqlParameter("@TaiKhoanGoc", ex.GetGuid(drhinhthucthanhtoangoc.SelectedValue));
                p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                p[10] = new SqlParameter("@Del", 1);
                p[10].SqlDbType = SqlDbType.Int;
                ex.ExecuteNonQuerry("sp_xdathanhtoanvaynganhang_Update", p);
                btnCancel_Click(sender, e);
                gstGetMess("Cập nhật thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình cập nhật, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tGiá Cước vận chuyển\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền sửa dữ liệu trên trang này", "");
    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            btnPre.Enabled = true;
            btnNext.Enabled = true;
            btnFirst.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GV.DataSource = ex.GetData("[sp_xdathanhtoanvaynganhang_List]", p);
            GV.DataBind();

        }
        else
        {
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("[sp_xdathanhtoanvaynganhang_List]", p);
        GV.DataBind();

        if (GV.Rows.Count.Equals(0))
        {
            btnPre.Enabled = true;
            btnNext.Enabled = false;
        }
        else
        {
            btnPre.Enabled = true;
            btnNext.Enabled = true;
        }
        btnFirst.Enabled = true;
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("[sp_xdathanhtoanvaynganhang_List]", p);
        GV.DataBind();

        btnPre.Enabled = false;
        btnNext.Enabled = true;
        btnFirst.Enabled = false;
    }

    void LoadNganHang()
    {
        dlnganhang.Items.Clear();
        dlnganhang.DataSource = ex.GetData("sp_thanhtoanvaynganhang_ListNganHang");
        dlnganhang.DataBind();
        dlnganhang.Items.Insert(0, new ListItem("--Chọn ngân hàng--", ""));
        dlnganhang.SelectedIndex = 0;
    }


    void LoadNganHang_TK()
    {
        DropDownList1.Items.Clear();
        DropDownList1.DataSource = ex.GetData("sp_thanhtoanvaynganhang_ListNganHang");
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--Chọn ngân hàng--", ""));
        DropDownList1.SelectedIndex = 0;
    }
    void LoadNhaCungCap(DropDownList dl, string title)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdanhacungcap_LoadDrop");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem(title, ""));
        dl.SelectedIndex = 0;
    }

    void LoadLoaiVatLieuAll(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdaloaivatlieu_LoadAll");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn vật liệu--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadNhaCungCapAll(DropDownList dl, string title)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdanhacungcap_LoadAll");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem(title, ""));
        dl.SelectedIndex = 0;
    }



    protected void dlnganhang_SelectedIndexChanged(object sender, EventArgs e)
    {
        p = new SqlParameter[3];
        p[0] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        p[1] = new SqlParameter("@Thang", dlthang.SelectedValue);
        p[1].SqlDbType = SqlDbType.Int;
        p[2] = new SqlParameter("@Nam", dlNam.SelectedValue);
        p[2].SqlDbType = SqlDbType.Int;
        dt = ex.GetData("sp_VayNganHang_TienGocDaTra", p);
        if (dt.Rows[0]["sotiengocdatra"].ToString().Equals(""))
            TextBox1.Text = "0";
        else
            TextBox1.Text = ConvertDecimal(double.Parse(dt.Rows[0]["sotiengocdatra"].ToString()));

        p = new SqlParameter[3];
        p[0] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        p[1] = new SqlParameter("@Thang", dlthang.SelectedValue);
        p[1].SqlDbType = SqlDbType.Int;
        p[2] = new SqlParameter("@Nam", dlNam.SelectedValue);
        p[2].SqlDbType = SqlDbType.Int;
        dt = ex.GetData("sp_VayNganHang_TienGocConLai", p);
        if (dt.Rows[0]["sotienconlai"].ToString().Equals(""))
            txtsotienconlai.Text = "0";
        else
            txtsotienconlai.Text = ConvertDecimal(double.Parse(dt.Rows[0]["sotienconlai"].ToString()));

        p = new SqlParameter[3];
        p[0] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        p[1] = new SqlParameter("@Thang", dlthang.SelectedValue);
        p[1].SqlDbType = SqlDbType.Int;
        p[2] = new SqlParameter("@Nam", dlNam.SelectedValue);
        p[2].SqlDbType = SqlDbType.Int;
        dt = ex.GetData("sp_VayNganHang_TienGocDaTraThangNay", p);
        if (dt.Rows[0]["sotiengoc"].ToString().Equals(""))
            txttiengocdatra.Text = "0";
        else
            txttiengocdatra.Text = ConvertDecimal(double.Parse(dt.Rows[0]["sotiengoc"].ToString()));


        p = new SqlParameter[3];
        p[0] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        p[1] = new SqlParameter("@Thang", dlthang.SelectedValue);
        p[1].SqlDbType = SqlDbType.Int;
        p[2] = new SqlParameter("@Nam", dlNam.SelectedValue);
        p[2].SqlDbType = SqlDbType.Int;
        dt = ex.GetData("sp_VayNganHang_TienLaiDaTraThangNay", p);
        if (dt.Rows[0]["sotiengoc"].ToString().Equals(""))
            txttienlaidatra.Text = "0";
        else
            txttienlaidatra.Text = ConvertDecimal(double.Parse(dt.Rows[0]["sotiengoc"].ToString()));

        p = new SqlParameter[1];
        p[0] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        
        dt = ex.GetData("sp_VayNganHang_TienGoc", p);
        if (dt.Rows[0]["tiengoc"].ToString().Equals(""))
            txttiengoc.Text = "0";
        else
            txttiengoc.Text = ConvertDecimal(double.Parse(dt.Rows[0]["tiengoc"].ToString()));

        p = new SqlParameter[3];
        p[0] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        p[1] = new SqlParameter("@Thang", dlthang.SelectedValue);
        p[1].SqlDbType = SqlDbType.Int;
        p[2] = new SqlParameter("@Nam", dlNam.SelectedValue);
        p[2].SqlDbType = SqlDbType.Int;
        dt = ex.GetData("sp_VayNganHang_TienGocTraMotThang", p);
        if (dt.Rows[0]["tiengoc"].ToString().Equals(""))
            txttiengocthangnay.Text = "0";
        else
            txttiengocthangnay.Text = ConvertDecimal(double.Parse(dt.Rows[0]["tiengoc"].ToString()));

        p = new SqlParameter[3];
        p[0] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        p[1] = new SqlParameter("@Thang", dlthang.SelectedValue);
        p[1].SqlDbType = SqlDbType.Int;
        p[2] = new SqlParameter("@Nam", dlNam.SelectedValue);
        p[2].SqlDbType = SqlDbType.Int;
        dt = ex.GetData("vaynganhang_TienLaiThangNay", p);
        if (dt.Rows[0]["tienlaithangnay"].ToString().Equals(""))
            txttienlaithangnay.Text = "0";
        else
            txttienlaithangnay.Text = ConvertDecimal(double.Parse(dt.Rows[0]["tienlaithangnay"].ToString()));
        string boiso;
        p = new SqlParameter[3];
        p[0] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        p[1] = new SqlParameter("@Thang", dlthang.SelectedValue);
        p[1].SqlDbType = SqlDbType.Int;
        p[2] = new SqlParameter("@Nam", dlNam.SelectedValue);
        p[2].SqlDbType = SqlDbType.Int;
        dt = ex.GetData("sp_VayNganHang_BoiSoTinhLai", p);
        if (dt.Rows[0]["boiso"].ToString().Equals(""))
            boiso = "0";
        else
            boiso = dt.Rows[0]["boiso"].ToString();
        #region tralai
        string kitralai;
         p = new SqlParameter[1];
        p[0] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        dt = ex.GetData("sp_xdavaynganhang", p);
        if (dt.Rows[0]["kitralai"].ToString().Equals(""))
            kitralai = "1";
        
             else
            kitralai = dt.Rows[0]["kitralai"].ToString();

        if (double.Parse(boiso) % double.Parse(kitralai) == 0 && double.Parse(boiso)!=0)
        {
            for (int i = 0; i < double.Parse(kitralai); i++)
            {

                p = new SqlParameter[3];
                p[0] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@Thang", int.Parse(dlthang.SelectedValue) - i);
                p[1].SqlDbType = SqlDbType.Int;
                p[2] = new SqlParameter("@Nam", dlNam.SelectedValue);
                p[2].SqlDbType = SqlDbType.Int;
                dt = ex.GetData("vaynganhang_TienLaiThangNay", p);
                if (dt.Rows[0]["tienlaithangnay"].ToString().Equals(""))
                    tienphaitra = "0";
                else
                    tienphaitra = ConvertDecimal(double.Parse(dt.Rows[0]["tienlaithangnay"].ToString()));
                tienlaiphaitra = tienlaiphaitra + double.Parse(tienphaitra);
            }

        }
        else tienlaiphaitra = 0;
        txttienlaiphaitra.Text = ConvertDecimal(tienlaiphaitra);
        #endregion
        #region tragoc
        string kitragoc;
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        dt = ex.GetData("[sp_xdavaynganhang_KiTraGoc]", p);
        if (dt.Rows[0]["kitragoc"].ToString().Equals(""))
            kitragoc = "1";

        else
            kitragoc = dt.Rows[0]["kitragoc"].ToString();

        if (double.Parse(boiso) % double.Parse(kitragoc) == 0 && double.Parse(boiso) != 0)
        {
            for (int i = 0; i < double.Parse(kitragoc); i++)
            {

                p = new SqlParameter[3];
                p[0] = new SqlParameter("@NganHang", ex.GetGuid(dlnganhang.SelectedValue));
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@Thang", int.Parse(dlthang.SelectedValue) - i);
                p[1].SqlDbType = SqlDbType.Int;
                p[2] = new SqlParameter("@Nam", dlNam.SelectedValue);
                p[2].SqlDbType = SqlDbType.Int;
                dt = ex.GetData("sp_VayNganHang_TienGocTraMotThang", p);
                if (dt.Rows[0]["tiengoc"].ToString().Equals(""))
                    tienphaitra = "0";
                else
                    tienphaitra = ConvertDecimal(double.Parse(dt.Rows[0]["tiengoc"].ToString()));
                tiengocphaitra = tiengocphaitra + double.Parse(tienphaitra);
            }

        }
        else tiengocphaitra = 0;
        txttiengocphaitra.Text = ConvertDecimal(tiengocphaitra);

        #endregion
    }

    string ConvertDecimal(double s)
    {
        string q = "";
        if (s.ToString().Contains("."))
        {
            double x = double.Parse(s.ToString().Substring(0, s.ToString().IndexOf('.')));
            q = (x != 0 && x > 999) ? string.Format("{0:0,0}", x) : x.ToString();
            q += s.ToString().Substring(s.ToString().IndexOf('.'));
        }
        else
            q = (s != 0 && s > 999) ? string.Format("{0:0,0}", s) : s.ToString();
        return q;
    }
    protected void dlthang_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(dlnganhang.SelectedIndex>0)
        dlnganhang_SelectedIndexChanged(sender, e);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex > 0)
        {
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@NganHang", int.Parse(DropDownList1.SelectedValue));
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;

            GV.DataSource = ex.GetData("sp_xdathanhtoanvaynganhang_List_TimKiem", p);
            GV.DataBind();
           
        }
    }
}